const { query } = require("./mysql");
const {
	YD_setSql,
	YD_datetimeToString,
	YD_whereSql,
} = require("lib/utils/common");

/** @name 添加进出记录 **/
exports.yd_add = (options) => {
	const sql = `insert into ch_record set ${YD_setSql(options)}`;
	return query(sql);
};

/** @name 查询进出记录-number **/
exports.yd_find = (options) => {
	const sql = `select
			yr.id, yr.number, yr.userId, yr.status, yr.orderCode, yr.pay, yr.payType, yr.note, yr.placeId,
			yp.code as placeCode, yp.type as placeType,
			${YD_datetimeToString("yr.startTime")},
			${YD_datetimeToString("yr.endTime")},
			${YD_datetimeToString("yr.updateTime")}
		from ch_record yr
		left join ch_place yp on yp.id=yr.placeId
		${YD_whereSql([
			{ field: "yr.number", type: "全等", value: options.number },
			{ field: "yr.status", type: "全等", value: options.status || 0 },
			{ field: "yr.endTime", type: "空", value: options.endTime || 0 },
		])}
	`;
	return query(sql);
};

/** @name 编辑进出记录 **/
exports.yd_update = (options, condition) => {
	const sql = `update ch_record set ${YD_setSql(options)} where id='${
		condition.id
	}'`;
	return query(sql);
};

/** @name 进出记录列表 **/
exports.yd_list = (options) => {
	const sql = `select
						yr.id, yr.number, yr.status, yr.orderCode, yr.pay, yr.payType, yr.note,
						yu.realname, yu.phone,
						yp.code as placeCode,
						${YD_datetimeToString("yr.startTime")},
						${YD_datetimeToString("yr.endTime")},
						${YD_datetimeToString("yr.updateTime")}
						from ch_record yr
						left join ch_user yu on yr.userId=yu.uuid
						left join ch_place yp on yr.placeId=yp.id
						${YD_whereSql([
							{ field: "yr.number", type: "模糊", value: options.number },
							{ field: "yu.realname", type: "模糊", value: options.realname },
							{ field: "yu.phone", type: "模糊", value: options.phone },
							{ field: "yr.status", type: "全等", value: options.status },
							{ field: "yr.orderCode", type: "模糊", value: options.orderCode },
							{ field: "yr.payType", type: "全等", value: options.payType },
							{ field: "yp.id", type: "全等", value: options.placeId },
						])}
						order by id desc
						limit ${(options.pageNum - 1) * options.pageSize},${options.pageSize}
            `;
	return query(sql);
};

/** @name 进出记录总量 **/
exports.yd_count = (options) => {
	const sql = `select count(*) as count
						from ch_record yr
						left join ch_user yu on yr.userId=yu.uuid
						left join ch_place yp on yr.placeId=yp.id
						${YD_whereSql([
							{ field: "yr.number", type: "模糊", value: options.number },
							{ field: "yu.realname", type: "模糊", value: options.realname },
							{ field: "yu.phone", type: "模糊", value: options.phone },
							{ field: "yr.status", type: "全等", value: options.status },
							{ field: "yr.orderCode", type: "模糊", value: options.orderCode },
							{ field: "yr.payType", type: "全等", value: options.payType },
							{ field: "yp.id", type: "全等", value: options.placeId },
						])}
            `;
	return query(sql);
};

/** @name 特定进出记录列表 **/
exports.yd_import = (options) => {
	const sql = `select
						yr.id, yr.number, yr.status, yr.orderCode, yr.pay, yr.payType, yr.note,
						yu.realname, yu.phone,
						yp.code as placeCode,
						yd.label as payStatus,
						${YD_datetimeToString("yr.startTime")},
						${YD_datetimeToString("yr.endTime")},
						${YD_datetimeToString("yr.updateTime")}
						from ch_record yr
						left join ch_user yu on yr.userId=yu.uuid
						left join ch_place yp on yr.placeId=yp.id
						left join (select * from ch_dictionary where type = 'pay_status') yd on yr.status=yd.value
						${YD_whereSql([{ field: "yr.id", type: "包含", value: options.ids }])}
						order by id desc
            `;
	return query(sql);
};
